package com.example.demo.controller;

import com.example.demo.common.ResultAjax;
import com.example.demo.model.Userinfo;
import com.example.demo.model.vo.ArticleinfoVO;
import com.example.demo.model.vo.UserinfoVO;
import com.example.demo.service.AdminService;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private ArticleService articleService;


    /**
     * 管理员管理用户
     * @return
     */
    @RequestMapping("/adminuser")
    public ResultAjax adminUserList() {
        List<Userinfo> list = adminService.getUserAll();
        list.remove(0);
        for (int i = 0; i < list.toArray().length; i++) {
            Userinfo userinfo = list.get(i);
            if (userinfo.getType().equals("reader")) {
                userinfo.setType("读者");
            }
            if (userinfo.getType().equals("writer")) {
                userinfo.setType("作者");
            }
            if (userinfo.getState().equals("1")) {
                userinfo.setState("正常");
            }
            if (userinfo.getState().equals("0")) {
                userinfo.setState("封禁中");
            }
            list.set(i,userinfo);
        }
        return ResultAjax.succ(list);
    }


    /**
     * 管理员封禁用户
     * 用唯一用户名来进行更改用户状态
     * @return
     */
    @RequestMapping("/close")
    public ResultAjax close(String username) {
        // 1.参数效验
        if (!StringUtils.hasLength(username)) {
            return ResultAjax.fail(-1,"参数错误");
        }
        // 2. 修改状态
        int result = adminService.updateClose(username);
        // 3. 返回
        return ResultAjax.succ(result);
    }


    /**
     * 管理员解封用户
     * 用唯一用户名来进行更改用户状态
     * @return
     */
    @RequestMapping("/open")
    public ResultAjax open(String username) {
        // 1.参数效验
        if (!StringUtils.hasLength(username)) {
            return ResultAjax.fail(-1,"参数错误");
        }
        // 2. 修改状态
        int result = adminService.updateOpen(username);
        // 3. 返回
        return ResultAjax.succ(result);
    }


    /**
     * 获取管理员审批列表
     * @return
     */
    @RequestMapping("/adminbook")
    public ResultAjax adminBookList() {
        List<ArticleinfoVO> list = adminService.getBookAll();
        for (int i = 0; i < list.toArray().length; i++) {
            ArticleinfoVO articleinfoVO = list.get(i);
            if (articleinfoVO.getState().equals("0")) {
                articleinfoVO.setState("申请中");
            }
            list.set(i,articleinfoVO);
        }
        return ResultAjax.succ(list);
    }


    /**
     * 管理员通过传书申请
     * @param articleinfoVO
     * @return
     */
    @RequestMapping("/pass")
    public ResultAjax pass(ArticleinfoVO articleinfoVO) {
        // 1. 参数效验
        if (articleinfoVO == null) {
            return ResultAjax.fail(-1,"参数错误!");
        }

        // 2. 把书籍查出来，并把申请记录删除
        ArticleinfoVO art = adminService.getApply(articleinfoVO);
        if (art == null) {
            return ResultAjax.fail(-1,"没有该记录");
        }
        adminService.del(articleinfoVO);

        // 3.加到书籍列表里,返回
        LocalDateTime dateTime = LocalDateTime.now();
        art.setUpdatetime(dateTime);
        int result = adminService.add(art);
        return ResultAjax.succ(result);
    }


    /**
     * 管理员拒绝传书
     * @param articleinfoVO
     * @return
     */
    @RequestMapping("/reject")
    public ResultAjax reject(ArticleinfoVO articleinfoVO) {
        // 1. 参数效验
        if (articleinfoVO == null) {
            return ResultAjax.fail(-1,"参数错误!");
        }
        // 2. 申请记录删除
        int result = adminService.del(articleinfoVO);
        // 3.返回
        return ResultAjax.succ(result);
    }


    /**
     * 获取作者注册的作者
     * @return
     */
    @RequestMapping("/adminwriter")
    public ResultAjax adminWriterList() {
        List<UserinfoVO> list = adminService.getWriterAll();
        for (int i = 0; i < list.toArray().length; i++) {
            UserinfoVO userinfoVO = list.get(i);
            if (userinfoVO.getState().equals("0")) {
                userinfoVO.setState("申请中");
            }
            list.set(i,userinfoVO);
        }
        return ResultAjax.succ(list);
    }

    /**
     * 通过作者注册申请
     * @param userinfoVO
     * @return
     */
    @RequestMapping("/writerpass")
    public ResultAjax writerPass(UserinfoVO userinfoVO) {
        // 1. 参数效验
        if (userinfoVO == null) {
            return ResultAjax.fail(-1,"参数错误");
        }
        // 2. 把作者查询出来,并把记录删除
        UserinfoVO user = adminService.getWriterApply(userinfoVO);
        if (user == null) {
            return ResultAjax.fail(-1,"没有该记录");
        }
        adminService.delWriterApply(userinfoVO);
        // 3. 把作者加入到用户表中
        user.setType("writer");
        LocalDateTime dateTime = LocalDateTime.now();
        int result = adminService.addWriter(user);
        return ResultAjax.succ(result);
    }

    /**
     * 拒绝作者注册申请
     * @param userinfoVO
     * @return
     */
    @RequestMapping("/writerreject")
    public ResultAjax writerReject(UserinfoVO userinfoVO) {
        // 1. 效验参数
        if (userinfoVO == null) {
            return ResultAjax.fail(-1,"参数错误");
        }
        // 2. 删除记录，返回
        int result = adminService.delWriterApply(userinfoVO);
        return ResultAjax.succ(result);
    }


}
